<template>
	<main-container>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div>
				<el-button type="primary" v-auth="'upms:sysdept:add'" @click="add">
					<el-icon>
						<ele-Plus />
					</el-icon>
					新增</el-button
				>
			</div>
			<right-toolbar :searchBtn="false" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<Form ref="refForm" :type="dialogType" @initPage="initPage"></Form>
		<!-- 列表 -->
		<el-table row-key="id" default-expand-all v-loading="loading" :data="tableData">
			<el-table-column prop="name" label="部门名称" />
			<el-table-column prop="leader" label="负责人" />
			<el-table-column prop="leaderPhone" label="负责人手机号" />
			<el-table-column prop="createTime" label="创建时间" />
			<el-table-column prop="sort" label="排序序号" />
			<el-table-column label="操作" width="200" align="center" fixed="right">
				<template #default="scope">
					<el-button link type="primary" v-auth="'upms:sysdept:edit'" @click="edit(scope.row)">
						<el-icon> <ele-Edit /> </el-icon> 修改
					</el-button>
					<el-button link type="danger" v-auth="'upms:sysdept:del'" @click="del(scope.row.id)">
						<el-icon> <ele-Delete /> </el-icon>
						删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
	</main-container>
</template>
<script lang="ts">
import { defineComponent, defineAsyncComponent, ref, reactive, toRefs } from 'vue';
import { getTreeList, getById, delObj } from '/@/api/upms/sysdept';
import { ElMessageBox, ElMessage } from 'element-plus';
export default defineComponent({
	components: { Form: defineAsyncComponent(() => import('./form.vue')) },
	setup() {
		const data = reactive({
			tableData: [],
		});
		const showSearch = ref(true);
		const loading = ref(false);
		const dialogType = ref('add');
		const refForm = ref();
		const { tableData } = toRefs(data);

		const initPage = async () => {
			loading.value = true;
			await getTreeList()
				.then((response: any) => {
					tableData.value = response.data.data;
					loading.value = false;
				})
				.catch(() => {
					loading.value = false;
				});
		};
		initPage();
		/**
		 * 新增按钮
		 */
		const add = () => {
			refForm.value.initForm();
			refForm.value.deptTree = tableData.value;
		};
		/**
		 * 修改按钮
		 */
		const edit = (row: any) => {
			refForm.value.initForm(row);
			refForm.value.deptTree = tableData.value;
		};
		/**
		 * 删除按钮
		 */
		const del = (id: string) => {
			ElMessageBox.confirm('此操作将删除该部门，是否继续?', '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delObj(id)
					.then((response: any) => {
						ElMessage.success('删除成功');
						initPage();
					})
					.catch(() => {});
			});
		};
		return {
			showSearch,
			loading,
			tableData,
			dialogType,
			refForm,
			initPage,
			add,
			edit,
			del,
		};
	},
});
</script>
